package com.iflytek.cuckoo.service.impl;

import com.iflytek.cuckoo.service.CacheService;
import com.iflytek.cuckoo.utils.JsonUtil;
import com.iflytek.cuckoo.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.Map;

/**
 * <p><一句话功能简述>
 * <p><功能详细描述>
 * <p>
 * <p>Copyright (c) 2018, liangzhang14@iflytek.com All Rights Reserve</p>
 * <p>Company : 科大讯飞</p>
 *
 * @author liangzhang14
 * @version [V1.0, 2019年04月17日 11:59]
 * @see [相关类/方法]
 */

@Service("cacheService")
public class RedisServiceImpl implements CacheService {

    @Autowired
    JedisPool jedisPool;


    private Jedis getJedis() {
        try {
            return jedisPool.getResource();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private void close(Jedis jedis) {
        if (jedis != null) jedis.close();
    }

    @Override
    public Jedis getJedisInstant() {
        return getJedis();
    }

    @Override
    public void watch(String... key) {
        Jedis jedis = getJedis();
        if (jedis != null)
            jedis.watch(key);
        close(jedis);
    }

    public void hash(String... key) {
        Jedis jedis = getJedis();
        if (jedis != null) {
            jedis.watch(key);
        }
        close(jedis);
    }

    @Override
    public void expire(String k, int tomeout) {
        Jedis jedis = getJedis();
        if (jedis != null)
            jedis.expire(k, tomeout);
        close(jedis);
    }

    @Override
    public void set(String key, String value) {
        Jedis jedis = getJedis();
        if (jedis != null)
            jedis.set(key, value);
        close(jedis);
    }

    @Override
    public void setDbIndex(String key, String value, int timeout, int dbIndex) {
        Jedis jedis = getJedis();
        if (jedis != null) {
            jedis.select(dbIndex);
            jedis.set(key, value);
            jedis.expire(key, timeout);
        }
        close(jedis);
    }


    @Override
    public void setDbIndex(String key, String value, int dbIndex) {
        Jedis jedis = getJedis();
        if (jedis != null) {
            jedis.select(dbIndex);
            jedis.set(key, value);
        }
        close(jedis);
    }

    @Override
    public void set(String key, String value, int timeout) {
        Jedis jedis = getJedis();
        if (jedis != null) {
            jedis.set(key, value);
            jedis.expire(key, timeout);
            close(jedis);
        }
    }

    @Override
    public String get(String key) {
        Jedis jedis = getJedis();
        try {
            if (jedis != null) {
                return jedis.get(key);
            }
        } finally {
            close(jedis);
        }
        return null;
    }

    @Override
    public String getDbIndex(String key, int dbIndex) {
        Jedis jedis = getJedis();
        try {
            if (jedis != null) {
                jedis.select(dbIndex);
                return jedis.get(key);
            }
        } finally {
            close(jedis);
        }
        return null;
    }

    @Override
    public void delete(String key) {
        Jedis jedis = getJedis();
        if (jedis != null)
            jedis.del(key);
        close(jedis);
    }

    @Override
    public void hset(String key, Object field, String value) {
        Jedis jedis = getJedis();
        if (jedis != null)
            jedis.hset(key, field + "", value);
        close(jedis);
    }

    @Override
    public String hget(String key, Object field) {
        Jedis jedis = getJedis();
        try {
            if (jedis != null)
                return jedis.hget(key, field + "");
        } finally {
            close(jedis);
        }
        return null;
    }

    @Override
    public Long hdel(String key, Object field) {
        Jedis jedis = getJedis();
        try {
            if (jedis != null)

                return jedis.hdel(key, field.toString());
        } finally {
            close(jedis);
        }
        return null;
    }

    @Override
    public String ttl(String phone) {
        Jedis jedisInstant = getJedis();
        String tt;
        try {
            if (jedisInstant != null) {
                jedisInstant = getJedisInstant();
                jedisInstant.select(1);
                tt = jedisInstant.ttl(phone).toString();
                return tt;
            }
        } finally {
            if (jedisInstant != null) {
                jedisInstant.close();
            }
        }
        return null;
    }

    @Override
    public <T> Object getObj(String key, Class<T> beanType) {
        String jsonData = get(key);
        if (StringUtils.isNotBlank(jsonData)) {
            return JsonUtil.json2Pojo(jsonData, beanType);
        }
        return null;
    }

    /*
     * @Describe:往list集合中添加数据
     * @author: hym in 2017/12/22
     */
    @Override
    public Long setList(String key, String value) {
        Jedis jedis = getJedis();
        try {
            if (jedis != null) {
                return jedis.rpush(key, value);
            }
        } finally {
            close(jedis);
        }
        return 0L;
    }

    //获取用户userToken
    @Override
    public Map<String, String> getToken(String ipackageId) {
        Map<String, String> headers = new HashMap<>();

        return headers;
    }
}
